angular.module('management', [])
    .controller('managementCtrl', ['$scope', 'httpService', 'alertmsg', '$timeout', '$rootScope', function($scope, httpService, alertmsg, $timeout, $rootScope) {
        $scope.isedit = false;

        // 获取部门
        $scope.getRoles = function(page) {
            if(!page) { page = 1 }
            if(!!$rootScope.user) {
                httpService.query('web/companies/' + $rootScope.user.company_id + '/fetch_department_roles', { page: page, limit: 9999}).then(function(res) {
                  if(res.code == 200) {
                    $scope.departments = res.departments;
                    $scope.manage(1);
                  }
                });
            }
        }
        $scope.getRoles();

        // 获取管理成员
        var pagenum = 1;
        var manages = {};
        $scope.manage = function(page) {
            if (page) {
                pagenum = page;
            }
            httpService.query('web/users/manager', { page: pagenum, limit: limit }).then(function(res) {
                if (res.code == 200) {
                	$scope.users = res.users;
                    angular.forEach($scope.users, function(v, k) {
                        if(!!!v.depart_role_id) {
                            v.depart_role_id = -1;
                        }
                        angular.forEach($scope.departments, function(a, b) {
                            if(v.user_type == 'manager' && !!v.account_id) {} else {
                                manages[v.id] = v;
                            }
                            if(v.department_id == a.id) {
                                v.department = a;
                                angular.forEach(a.depart_roles, function(c, d) {
                                    if(c.id == v.depart_role_id) {
                                        v.department.role_name = c.name;
                                    }
                                });
                            }
                        });
                    });
                	$scope.page = res.page;
                }
            })
        };
        

        // 设置角色
        var types = [];
        var ids = [];
        $scope.setUserType = function(user, index) {
            manages[user.id].department_id = user.department.id;
            manages[user.id].depart_role_id = user.depart_role_id;
        };

        $scope.saveType = function() {
            for(var v in manages) {
                var u = manages[v];
                var idx = types.indexOf(u);
                if(idx == -1) {
                    types.push({ user_id: u.id, department_id: u.department_id, depart_role_id: u.depart_role_id });
                }
            }
            httpService.query('web/companies/' + user.company_id + '/allot_roles', { users: types }, 'post').then(function(res) {
                if(res.code == 200) {
                    types = [];
                    manages = {};
                    $scope.isedit = false;
                    $scope.manage(1);
                } else {
                    alertmsg(res.msg);
                }
            });
        };

    }]);
